// 员工信息表
import { getURL, getToken, $http } from './api.js';
import { getDocuments, createDocument, clearDocuments, timeToStamp } from '../index.js';
(function(runTime) {
  var formName = '员工信息表';
  clearDocuments(formName);
  // 获取员工列表，执行入库
  var shopList = getDocuments({
    formName: '门店信息表'
  })
  for (var i = 0; i < shopList.size(); i++) {
    var shopId = shopList.get(i).getElementByName('门店编号').getValue();
    var docId = shopList.get(i).id;
    getStaffList(shopId, docId, 1);
  }
  function getStaffList(shopId, docId, page) {
    page = page || 1;
    var uri = getURL('/scrm/inside/shop-staff-list');
    var params = {
      access_token: getToken(),
      shop_id: shopId,
      page
    }
    var resData = $http.get({
      uri,
      params,
    })
    var data = resData.data;
    var list = data.data;
    // 如果请求失败，进行重试
    if (resData.status != 'ok') {
      getStaffList(shopId, docId, page);
      return;
    }
    // 如果有下一页，继续获取
    if (data.last_page > data.current_page) {
      getStaffList(shopId, docId, ++page);
    }
    // 循环塞数据
    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      createDocument({
        formName,
        formData: {
          '所属门店': docId,
          '所属门店编号': shopId,
          '员工姓名': item.real_name,
          '员工企微标识': item.user_id,
          '联系方式': item.mobile,
          '员工状态': item.status,
          '员工职称': item.position,
          '顾客同步时间': timeToStamp(runTime) + i * 60000
        }
      })
    }
  }
})('16:10:00')
